require(["../../conf/config.js"],function(){
	require(["loginTop","itemsMsg"],function(loginTop,itemsMsg){
		loginTop();
		let num = document.getElementsByTagName("i")[0].innerText;
		console.log(num)
		itemsMsg(num);
	})
	
	//加入购物车
	require(["loginTop","itemsMsg","setCookie","getCookie"],function(loginTop,itemsMsg,setCookie,getCookie){
		let add_item = document.getElementById("add-buyCar");
		
		add_item.addEventListener("click",function(){
			//拿到商品的名字，价格和数量
			let item_img = document.getElementsByClassName("item-left")[0].children[0].src;
			let item_name = document.getElementsByClassName("item-right")[0].children[0].innerText;
			let item_price = document.getElementById("item-price").innerText;
			let item_count = document.getElementById("item-count").value;
			/*console.log(item_name);
			console.log(item_price);*/
			
			//使用数组存储对象
			let items = { //创建商品列表对象,并将本次的数据赋值
				_img : item_img,
				_name : item_name,
				_price : item_price,
				_count : 1
			}
			
			if(!getCookie("shoppingCar")){
				//如果是第一次点击购物车
				console.log("第一次点击");
				var shoppingsList = []; //创建一个数组
				shoppingsList.push(items); //将商品加入数组
				console.log(shoppingsList)
			}else{
				//不是第一次添加，从cookie中取出,getCookie返回的是数组
				console.log(Array.from(JSON.parse(getCookie("shoppingCar")[1])));
				shoppingsList = (JSON.parse(getCookie("shoppingCar")[1]));
				
				let isExist = false; //判断是否已经存在该商品
				shoppingsList.forEach(function(item){
					//console.log(item._name);
					if(item._name == item_name){ //如果名字相同
						isExist = true;
						//console.log("已存在")
						item._count ++ ;
					}
				});
				if(!isExist){ //如果存在，isExist为true，不添加
					shoppingsList.push(items);
				}
			}
			
			//数组需要转换为字符串
			setCookie("shoppingCar",JSON.stringify(shoppingsList),10);
			
		})
		
	})
	
})